package cn.db101.view;

import cn.db101.dao.shelfDao;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class add_bookstyle_Frame{
	 JFrame jf=new JFrame() ;
	private JTextField newcatagory_TextField;
	private JTextField oldcatagory_textField;
	private JTextField location_textField;

	

	/**
	 * Create the frame.
	 * @wbp.parser.entryPoint
	 */
	public void init() {
		jf.setTitle("\u4FEE\u6539\u56FE\u4E66\u7C7B\u578B");
		jf.setBounds(100, 100, 505, 653);
		
		JPanel panel = new JPanel();
		jf.getContentPane().add(panel, BorderLayout.CENTER);
		
		JPanel panel_1 = new JPanel();
		panel_1.setToolTipText("");
		panel_1.setForeground(new Color(0, 0, 0));
		panel_1.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u4FEE\u6539\u56FE\u4E66\u7C7B\u578B", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(0, 0, 0)));
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(add_bookstyle_Frame.class.getResource("/img/x.jpg")));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addComponent(lblNewLabel)
				.addGroup(gl_panel.createSequentialGroup()
					.addGap(27)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 442, GroupLayout.PREFERRED_SIZE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addComponent(lblNewLabel, GroupLayout.PREFERRED_SIZE, 258, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, 342, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(58, Short.MAX_VALUE))
		);
		
		JLabel label = new JLabel("\u8BF7\u8F93\u5165\u7C7B\u540D\uFF1A");
		label.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		JLabel label_1 = new JLabel("\u65B0\u7C7B\u540D\u4E3A\uFF1A");
		label_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		newcatagory_TextField = new JTextField();
		newcatagory_TextField.setColumns(10);
		
		JButton addcatagory_Button = new JButton("\u6DFB\u52A0");
		addcatagory_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addAction(e);
			}
		});
		addcatagory_Button.setIcon(new ImageIcon(add_bookstyle_Frame.class.getResource("/icon/plus\u6DFB\u52A0.png")));
		
		JButton modifycatagory_Button = new JButton("\u4FEE\u6539");
		modifycatagory_Button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				modifyAction(e);
			}
		});
		modifycatagory_Button.setIcon(new ImageIcon(add_bookstyle_Frame.class.getResource("/icon/edit\u7F16\u8F91.png")));
		
		oldcatagory_textField = new JTextField();
		oldcatagory_textField.setColumns(10);
		
		JLabel label_1_1 = new JLabel("\u6DFB\u52A0\u5730\u70B9\uFF1A");
		label_1_1.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		
		location_textField = new JTextField();
		location_textField.setColumns(10);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
					.addContainerGap(246, Short.MAX_VALUE)
					.addComponent(modifycatagory_Button, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE)
					.addGap(101))
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(103)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
						.addComponent(addcatagory_Button)
						.addGroup(gl_panel_1.createSequentialGroup()
							.addGroup(gl_panel_1.createParallelGroup(Alignment.TRAILING)
								.addComponent(label_1)
								.addComponent(label)
								.addComponent(label_1_1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
								.addComponent(location_textField, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
								.addGroup(gl_panel_1.createParallelGroup(Alignment.LEADING)
									.addComponent(newcatagory_TextField, GroupLayout.PREFERRED_SIZE, 97, GroupLayout.PREFERRED_SIZE)
									.addComponent(oldcatagory_textField, GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)))))
					.addContainerGap(118, Short.MAX_VALUE))
		);
		gl_panel_1.setVerticalGroup(
			gl_panel_1.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel_1.createSequentialGroup()
					.addGap(45)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label)
						.addComponent(oldcatagory_textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(newcatagory_TextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(18)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(label_1_1, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
						.addComponent(location_textField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
					.addGap(83)
					.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(modifycatagory_Button, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE)
						.addComponent(addcatagory_Button, GroupLayout.PREFERRED_SIZE, 36, GroupLayout.PREFERRED_SIZE))
					.addGap(56))
		);
		panel_1.setLayout(gl_panel_1);
		panel.setLayout(gl_panel);
		//���ô����Ƿ�ɸı�
		jf.setResizable(false);
		//���ô���ɼ�
		jf.setVisible(true);	
		//����JFrame������ʾ
		jf.setLocationRelativeTo(null);

	}



	protected void modifyAction(ActionEvent e) {
		String oldCatagory = oldcatagory_textField.getText();
		String newCatagory = newcatagory_TextField.getText();
		if(oldCatagory.equals("") || oldCatagory == null) {
			JOptionPane.showMessageDialog(null, "���벻��Ϊ��");
			return;
		}
		if(newCatagory.equals("") || newCatagory == null) {
			JOptionPane.showMessageDialog(null, "���벻��Ϊ��");
			return;
		}
		if(new shelfDao().alterCatagory(oldCatagory, newCatagory) == 1) {
			JOptionPane.showMessageDialog(null, "���ĳɹ�");
			return;
		}else {
			JOptionPane.showMessageDialog(null, "δ�ҵ�����");
			return;
		}
		
	}


	protected void addAction(ActionEvent e) {
		// TODO Auto-generated method stub
		String newCatagory = newcatagory_TextField.getText();
		String newlocation = location_textField.getText();
		if(newlocation.equals("") || newlocation == null) {
			JOptionPane.showMessageDialog(null, "���벻��Ϊ��");
			return;
		}
		if(newCatagory.equals("") || newCatagory == null) {
			JOptionPane.showMessageDialog(null, "���벻��Ϊ��");
			return;
		}
		if(new shelfDao().addCatagory(newCatagory, newlocation) == 1) {
			JOptionPane.showMessageDialog(null, "����ɹ�");
			return;
		}else {
			JOptionPane.showMessageDialog(null, "����ʧ��");
			return;
		}
	}
	
	public static void main(String[] args) {
		new add_bookstyle_Frame().init();
	}
}
